<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @property Doctrine $doctrine Biblioteca ORM
 * Controller inicial
 */
class MenuPrincipal extends AdminController {
    /**
     * Metodo para listar os cadastros
     *
     * @param int $offset Registro inicial
     */
    public function index( $page = 0 ){
        $findBy = array();
        $offset = 0;
        if( $page != 0 ) {
            $offset = ( $page - 1 ) * LIMITE_PAGINACAO_ADMIN;
        }

        $lista = $this->doctrine->em->getRepository( 'MenusPrincipais' )->findBy(
            $findBy, array('ordem' => 'ASC'), LIMITE_PAGINACAO_ADMIN, $offset
        );

        $this->assign( 'lista', $lista );
        $this->assign( 'total', $this->count() );
        $this->display( 'menuprincipal/index' );
    }
    
    public function editar($id = null) {
        $obj = new MenusPrincipais();
        if (!empty($id) && $_SERVER['REQUEST_METHOD'] == 'GET') {
            $obj = $this->doctrine->em->getRepository('MenusPrincipais')->find($id);
        }
        $_POST = $obj->asArray();
        $this->display('menuprincipal/editar');
    }
    
    public function save( $id = null ){
        $em = $this->doctrine->em;
        $em->getConnection()->beginTransaction(); 
        $entity = new MenusPrincipais();
        try {
            if( !empty( $id ) ) {
                $entity = $em->getRepository( 'MenusPrincipais' )->find( $id );
            }
            populateForm( $entity, $this->input->post() );
            if ($this->validate()){
                $em->persist( $entity );
                $this->updateOrder($entity->get_ordem());
                $em->flush();
                $em->getConnection()->commit();
                $this->showSuccess();
                redirect('admin/menuprincipal/index');
            } 
        } catch (Exception $ex) {
            $em->getConnection()->rollback();
            $em->close();
            $this->showErros($ex);
        }
        $this->display('menuprincipal/editar');
    }
    
    private function validate(){
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('titulo', 'Título', 'required');
        $this->form_validation->set_rules('ordem', 'Ordem', 'required|greater_than[0]');
        return $this->form_validation->run();
    }
    
    private function setFlash($key, $value){
        $this->session->set_flashdata($key, $value);
    }
    
    private function updateOrder($ordem){
        if ($this->existsOrder($ordem)){
            $sql = "update menus_principais set ordem = ordem + 1 where ordem >= $ordem";
            $stmt = $this->doctrine->em->getConnection()->prepare($sql);
            $stmt->execute();
        }
    }
    
    private function existsOrder($ordem){
        $sql = "select 1 from menus_principais m where m.ordem = $ordem";
        $stmt = $this->doctrine->em->getConnection()->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetchAll();
        return !empty($result);
    }
    
    public function count() {
        $sql = "select count(*) from menus_principais";
        $stmt = $this->doctrine->em->getConnection()->prepare($sql);
        $stmt->execute();
        return $stmt->fetchColumn(0);
    }

}
/* End of file home.php */
/* Location: ./application/controllers/admin/home.php */